"""
英语问题生成工具，根据CEFR级别生成不同难度的英语问题。
"""
import random
from typing import Dict, List, Any, Optional

# CEFR级别描述，用于提示大模型生成合适难度的问题
CEFR_DESCRIPTIONS = {
    "A1": "入门级。能理解和使用日常用语和非常基本的短语，能介绍自己和他人，能就个人细节提问和回答。",
    "A2": "基础级。能理解与自己直接相关的句子和常用表达，能进行简单的信息交换。",
    "B1": "中级。能理解日常生活中的主要内容，能处理旅行中可能出现的大多数情况，能就熟悉的话题进行简单的交流。",
    "B2": "中高级。能理解复杂文本的主要思想，能与母语人士进行流利的交流，能就广泛的话题提出清晰的观点。",
    "C1": "高级。能理解各种复杂的长篇文本，能流利自然地表达，能灵活有效地使用语言进行社交、学术和专业交流。",
    "C2": "精通级。能轻松理解几乎所有听到或读到的内容，能从不同来源的书面和口头信息中归纳出信息，能流利地重构论点。"
}

# 题型定义
QUESTION_TYPES = ["multiple_choice", "fill_in_blank"]

def generate_english_question(cefr_level: str, question_type: Optional[str] = None) -> Dict[str, Any]:
    """
    根据CEFR级别生成英语问题。
    
    Args:
        cefr_level: CEFR级别，可选值为 "A1", "A2", "B1", "B2", "C1", "C2"
        question_type: 题目类型，可选值为 "multiple_choice" 或 "fill_in_blank"，如果不指定则随机选择
        
    Returns:
        包含问题内容的字典，格式如下：
        {
            "question_type": "multiple_choice" 或 "fill_in_blank",
            "question": "问题内容",
            "options": ["选项A", "选项B", "选项C", "选项D"] (仅当question_type为multiple_choice时),
            "correct_answer": "正确答案",
            "explanation": "答案解释"
        }
    """
    # 验证CEFR级别
    if cefr_level not in CEFR_DESCRIPTIONS:
        return {
            "status": "error",
            "message": f"无效的CEFR级别: {cefr_level}。有效级别为: {', '.join(CEFR_DESCRIPTIONS.keys())}"
        }
    
    # 如果未指定题型，随机选择一种
    if question_type is None:
        question_type = random.choice(QUESTION_TYPES)
    elif question_type not in QUESTION_TYPES:
        return {
            "status": "error",
            "message": f"无效的题型: {question_type}。有效题型为: {', '.join(QUESTION_TYPES)}"
        }
    
    # 这里我们会使用大模型来生成问题，但在这个函数中我们只返回一个示例
    # 在实际实现中，这里会调用大模型API来生成问题
    
    # 示例问题（实际应用中会被大模型生成的内容替代）
    sample_questions = {
        "A1": {
            "multiple_choice": {
                "question": "What is she doing?",
                "options": ["She is running.", "She is sleeping.", "She is eating.", "She is working."],
                "correct_answer": "She is running.",
                "explanation": "The correct answer is 'She is running.' This uses the present continuous tense to describe an action happening now."
            },
            "fill_in_blank": {
                "question": "I ___ from China.",
                "correct_answer": "am",
                "explanation": "The correct answer is 'am'. We use 'am' with the pronoun 'I' in the present tense of the verb 'to be'."
            }
        },
        "B1": {
            "multiple_choice": {
                "question": "If I ___ enough money, I would buy a new car.",
                "options": ["have", "had", "will have", "would have"],
                "correct_answer": "had",
                "explanation": "The correct answer is 'had'. This is a conditional sentence (type 2) that expresses an unreal or hypothetical situation in the present."
            },
            "fill_in_blank": {
                "question": "She has been working here ___ 2015.",
                "correct_answer": "since",
                "explanation": "The correct answer is 'since'. We use 'since' with a specific point in time when something began."
            }
        },
        "C1": {
            "multiple_choice": {
                "question": "The government announced that taxes ___ by 2% next year.",
                "options": ["will increase", "will be increased", "are increasing", "are going to increase"],
                "correct_answer": "will be increased",
                "explanation": "The correct answer is 'will be increased'. This is the passive voice in the future simple tense, appropriate when the focus is on the action rather than who performs it."
            },
            "fill_in_blank": {
                "question": "Despite ___ all night, she still failed the exam.",
                "correct_answer": "studying",
                "explanation": "The correct answer is 'studying'. After 'despite', we use a noun, gerund (-ing form), or a noun phrase."
            }
        }
    }
    
    # 根据CEFR级别选择合适的示例问题
    # 简化处理：将A2映射到A1，B2映射到B1，C2映射到C1
    level_map = {"A1": "A1", "A2": "A1", "B1": "B1", "B2": "B1", "C1": "C1", "C2": "C1"}
    mapped_level = level_map[cefr_level]
    
    # 获取对应级别和题型的问题
    question_data = sample_questions[mapped_level][question_type].copy()
    
    # 添加元数据
    question_data["cefr_level"] = cefr_level
    question_data["question_type"] = question_type
    question_data["status"] = "success"
    
    return question_data